package ait.mediabroadcast;

import org.teleal.cling.UpnpService;
import org.teleal.cling.android.AndroidUpnpServiceConfiguration;
import org.teleal.cling.android.AndroidUpnpServiceImpl;
import org.teleal.cling.android.AndroidWifiSwitchableRouter;
import org.teleal.cling.model.ModelUtil;

import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.util.Log;

public class IlMioAndroidUpnpService extends AndroidUpnpServiceImpl {

	@Override
	protected AndroidUpnpServiceConfiguration createConfiguration(WifiManager wifiManager) {
		Log.v("AIT", "======= creating configuration");
		return new AndroidUpnpServiceConfiguration(wifiManager, 9000) {
			
			@Override
			public int getRegistryMaintenanceIntervalMillis() {
				return 10000;
			}
		};
	}

	@Override
	public void onDestroy() {
//		super.onDestroy();
		
		if (!ModelUtil.ANDROID_EMULATOR && isListeningForConnectivityChanges())
	           unregisterReceiver(((AndroidWifiSwitchableRouter) upnpService.getRouter()).getBroadcastReceiver());
			new Shutdown().execute(upnpService); 
	}
	
	private class Shutdown extends AsyncTask<UpnpService, Integer, Void> {

	    @Override protected Void doInBackground(UpnpService... svcs) {
	    	UpnpService svc = svcs[0];
		    if (null != svc) 
		    	svc.shutdown();
		    return null;
	    } 
	}
	
}
